package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IDifferentiateBrandService;
import com.mdd.admin.validate.DifferentiateBrandCreateValidate;
import com.mdd.admin.validate.DifferentiateBrandUpdateValidate;
import com.mdd.admin.validate.DifferentiateBrandSearchValidate;
import com.mdd.admin.vo.DifferentiateBrandListedVo;
import com.mdd.admin.vo.DifferentiateBrandDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.DifferentiateBrand;
import com.mdd.common.mapper.DifferentiateBrandMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 品牌管理实现类
 * @author LikeAdmin
 */
@Service
public class DifferentiateBrandServiceImpl implements IDifferentiateBrandService {
        
    @Resource
    DifferentiateBrandMapper differentiateBrandMapper;

    /**
     * 品牌管理列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<DifferentiateBrandListedVo>
     */
    @Override
    public PageResult<DifferentiateBrandListedVo> list(PageValidate pageValidate, DifferentiateBrandSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<DifferentiateBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        differentiateBrandMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:brandName@brand_name:str",
            "=:status:str",
            "=:createId@create_id:int",
            "like:createName@create_name:str",
            "=:evaluateMoney@evaluate_money:str",
            "=:type:str",
        });

        IPage<DifferentiateBrand> iPage = differentiateBrandMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<DifferentiateBrandListedVo> list = new LinkedList<>();
        for(DifferentiateBrand item : iPage.getRecords()) {
            DifferentiateBrandListedVo vo = new DifferentiateBrandListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(new Date());
            vo.setUpdateTime(new Date());
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 品牌管理详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return DifferentiateBrand
     */
    @Override
    public DifferentiateBrandDetailVo detail(String id) {
        DifferentiateBrand model = differentiateBrandMapper.selectOne(
                new QueryWrapper<DifferentiateBrand>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        DifferentiateBrandDetailVo vo = new DifferentiateBrandDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 品牌管理新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(DifferentiateBrandCreateValidate createValidate) {
        DifferentiateBrand model = new DifferentiateBrand();
        model.setBrandName(createValidate.getBrandName());
        model.setStatus(createValidate.getStatus());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        model.setCreateId(createValidate.getCreateId());
        model.setCreateName(createValidate.getCreateName());
        model.setEvaluateMoney(createValidate.getEvaluateMoney());
        model.setType(createValidate.getType());
        differentiateBrandMapper.insert(model);
    }

    /**
     * 品牌管理编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(DifferentiateBrandUpdateValidate updateValidate) {
        DifferentiateBrand model = differentiateBrandMapper.selectOne(
                new QueryWrapper<DifferentiateBrand>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setBrandName(updateValidate.getBrandName());
        model.setStatus(updateValidate.getStatus());
        model.setUpdateTime(new Date());
        model.setCreateId(updateValidate.getCreateId());
        model.setCreateName(updateValidate.getCreateName());
        model.setEvaluateMoney(updateValidate.getEvaluateMoney());
        model.setType(updateValidate.getType());
        differentiateBrandMapper.updateById(model);
    }

    /**
     * 品牌管理删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(String id) {
        DifferentiateBrand model = differentiateBrandMapper.selectOne(
                new QueryWrapper<DifferentiateBrand>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        differentiateBrandMapper.delete(new QueryWrapper<DifferentiateBrand>().eq("id", id));
    }

}
